var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    issuccessMask: false,
    ypage:false
  },

  priviewdesign(){
    let pages = getCurrentPages();
    if(pages.length==5){
      wx.navigateTo({
        url: '/pages/projectDetails/projectDetails?orderId='+this.data.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/projectDetails/projectDetails?orderId='+this.data.id,
      })
    }
   

  },


  // 项目详情接口
  getOrderDetails(orderId) {
    let t = this
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getOrderDetails',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: 1569562832576524288,
        orderId: orderId,
      },
      success: res => {
        wx.hideLoading();
        console.log(res.data.designList)
        let shopowner = res.data.designList
        let product = []
        shopowner.forEach(item => {
          let obj = {}
          obj.productModelobj = {
            model: item.productModel,
            name: item.productModel+" "+item.productName
          }
          obj.productImage = []
          item.productImage.split(",").forEach(item => obj.productImage.push({
            image: item,
            ischeck: true
          }))
          obj.number = item.productModelNum
          product.push(obj)
        })
        let depositprictrueList = []
        let explain = shopowner[0].designExplain
        res.data.projectOrder.projectDesignDraw.split(",").forEach(item => depositprictrueList.push(this.data.Imageurl + item))

        this.setData({
          product: product,
          depositprictrueList: depositprictrueList,
          explain: explain
        })


      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },



  // 上传附件
  upload(arr) {
    // console.log(arr)
    let t = this
    return new Promise((reslove, reject) => {
      if (arr.length == 0) {
        reslove([])
        return
      }
      wx.showLoading({
        title: '上传中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      let finishFilesList = []
      arr.forEach((item, index) => {
        wx.uploadFile({
          url: app.globalData.Interfaceurl + 'saveFileUpload',
          filePath: item,
          name: 'file',
          formData: {
            userId: app.globalData.userId,

          },
          header: {
            "content-type": "multipart/form-data"
          },
          success: function (res) {
            finishFilesList.push(JSON.parse(res.data).fileUrl)
            if (index >= arr.length - 1) {
              setTimeout(()=>{
                wx.hideLoading();
                reslove(finishFilesList)
               },500)
            }
          },
          fail: err => {
            return err
          }
        })
      })
    })


  },
  // 上传设计图
  saveOrderDesign() {
    let depositprictrueList = this.data.depositprictrueList
    let t = this
    this.upload(depositprictrueList)
      .then((res) => {
        let newdepositprictrueList = res.join()
        let modelArr = []
        this.data.product.forEach(item => {
          let obj = {}
          obj.specId = item.specificationsobj.id
          obj.seriesId = item.productSeriestwoobj.id
          obj.modelId = item.productModelobj.id
          obj.modelNum = item.number
          let productImage = item.productImage.filter(item => item.ischeck == true)
          let newproductImage = []
          productImage.forEach(item => newproductImage.push(item.id))
          obj.imageId = newproductImage.join(",")
          modelArr.push(obj)
        })
        console.log(modelArr)
        wx.showLoading({
          title: '加载中...', //提示的内容
          mask: true //显示透明蒙层 防止触摸穿透
        })

        wx.request({
          url: app.globalData.Interfaceurl + 'saveOrderDesign',
          method: "post",
          header: {
            "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          data: {

            userId: app.globalData.userId,
            // userId: 1569562832576524288,
            orderId: t.data.id,
            token: t.data.token,
            designExplain: t.data.explain,
            designDraw: newdepositprictrueList,
            modelArr: JSON.stringify(modelArr),

          },
          success: res => {
            wx.hideLoading();
            console.log(res)
            if (res.data.result == -1) {
              wx.showToast({
                duration: 2500, //持续的时间
                icon: 'none', //图标
                title: res.data.message,
              })
              t.setData({
                token:app.token()
              })

            } else {
              this.setData({
                issuccessMask: true
              })
            }
          },
          fail: err => {
            wx.showToast({
              title: '请求失败',
              duration: 2500, //持续的时间
              icon: 'none', //图标
            })
          }
        })


      })



  },

  HidedispatcCar() {

    wx.setStorageSync('istab', true)
    this.setData({
      issuccessMask: false,
      todo:'priviewdesign',
      ypage:true
    })
   
  },


  modify(e) {
   if(this.data.todo){
     return
   }
    let url = e.currentTarget.dataset.url
    if (url == 'productMap') {
      wx.navigateTo({
        url: '/shopowner/pages/productMap/productMap?todo=modify&&product=' + JSON.stringify(this.data.product),
      })
    } else if (url == 'designchart') {
      wx.navigateTo({
        url: '/shopowner/pages/designchart/designchart?todo=modify&&depositprictrueList=' + JSON.stringify(this.data.depositprictrueList) + '&&explain=' + this.data.explain,
      })
    }
  },
  priview(e) {
    let item = e.currentTarget.dataset.item
    let imagesindex = e.currentTarget.dataset.imagesindex

    let urls = []
    item.productImage.forEach(item => urls.push(this.data.Imageurl + item.image))
    console.log(urls)
    let current = urls[imagesindex]
    wx.previewImage({
      current: current, // 当前显示图片的 http 链接
      urls: urls, // 需要预览的图片 http 链接列表

    })
  },
  priviewdepositpric(e) {

    let imagesindex = e.currentTarget.dataset.imagesindex

    let urls = this.data.depositprictrueList
    let current = urls[imagesindex]
    wx.previewImage({
      current: current, // 当前显示图片的 http 链接
      urls: urls, // 需要预览的图片 http 链接列表

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)








    if (options.todo) {
      this.setData({
        todo: options.todo,
        Imageurl: app.globalData.Imageurl,
        id:options.orderId,
      })
      console.log(options)
      this.getOrderDetails(options.orderId)
    } else {
      this.setData({
        product: JSON.parse(options.product),
        explain: options.explain,
        depositprictrueList: JSON.parse(options.depositprictrueList),
        id: options.id,
        Imageurl: app.globalData.Imageurl,
        token: app.token()
      })
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})